﻿

window.JIM = new JMessage({
    //debug: true
});

function bluesendtext(e) {
    var p = $(e).prev();
    var text = p.val();
    p.val('');
    sendGroupMsg(text);
}

function redsendtext(e) {
    var p = $(e).prev();
    var text = p.val();
    p.val('');
    sendGroupMsg(text,1);
}

JIM.onDisconnect(function () {
    appendToDashboard(username + ":已断线,请刷新页面后再试", 1);
    appendToDashboard(username + ":已断线,请刷新页面后再试");
}); //异常断线监听

function appendToDashboard(text, l) {
    if (l) {
        $('#dashboardred').append('<li>' + text + '</li>');
    } else {
        $('#dashboardblue').append('<li>' + text + '</li>');
    }  
    
}
setTimeout(init, 3000);

function init() {
    $.getJSON('/Platform/GetSignature', function (data) {
        if (data.success) {
            JIM.init(data.result).onSuccess(function (data) {
                login();
            }).onFail(function (data) {
                console.log('error:' + JSON.stringify(data))
                appendToDashboard('error: ' + JSON.stringify(data));
            });
        }
    })
}

function loginOut() {
    JIM.loginOut();
}

function login() {
    JIM.login({
        'username': across_user,
        'password': across_user_password
    }).onSuccess(function (data) {
        appendToDashboard("【" + username + "】登录成功");
        appendToDashboard("【" + username + "】登录成功", 1);
        JIM.onMsgReceive(function (data) {
            console.log("onMsgReceive=" + JSON.stringify(data))
            var messages = data.messages;
            
            $.each(messages, function (i, m) {
                $.post('/Platform/GetUserName', { guid: m.content.from_name }, function (data) {
                    if (data.success) {
                        if (m.content.target_id == '22779411') {
                            appendToDashboard(data.Name + ":" + m.content.msg_body.text);
                            return;
                        }

                        if (m.content.target_id == '22779399') {
                            appendToDashboard(data.Name + ":" + m.content.msg_body.text, 1);
                            return;
                        }
                        
                    }
                })
                
            })
            
        });

        JIM.onEventNotification(function (data) {
            console.log('onEventNotification: ' + JSON.stringify(data));
            appendToDashboard('onEventNotification: ' + JSON.stringify(data));
        });

        JIM.onSyncConversation(function (data) { //离线消息同步监听
            console.log('onSyncConversation: ' + JSON.stringify(data));
            appendToDashboard('onSyncConversation: ' + JSON.stringify(data));
        });

    }).onFail(function (data) {
        console.log('error:' + JSON.stringify(data));
        appendToDashboard('error: ' + JSON.stringify(data));
    }).onTimeout(function (data) {
        console.log('timeout:' + JSON.stringify(data));
        appendToDashboard('timeout: ' + JSON.stringify(data));
    });
}


function sendGroupMsg(text, l) {
    var target_gid = "22779411",
        target_gname = "蓝方";
    if (l) {
        target_gid = '22779399',
        target_gname = "红方";
    }
    JIM.sendGroupMsg({
        'target_gid': target_gid,
        'target_gname': target_gname,
        'content': text,
        //'at_list': [{ 'username': across_user, 'appkey': across_appkey }]
    }).onSuccess(function (data) {
        appendToDashboard(username + ":" + text, l);
    }).onFail(function (data) {
        console.log('error:' + JSON.stringify(data));
        appendToDashboard('error: ' + JSON.stringify(data));
    });
}


function on_return(e,l) {
    if (window.event.keyCode == 13) {
        sendGroupMsg($(e).val(), l);
        $(e).val('');
    }
}

